package com.ruoyi.service;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.Area;
import com.ruoyi.common.core.mapper.AreaMapper;
import com.ruoyi.common.core.service.BaseServiceImpl;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;


/**
 * 【请填写功能名称】
 * @date 2025-03-13
 */
@Service
public class AreaServiceImpl  extends BaseServiceImpl<AreaMapper, Area>
{

    public void selectPage(Page page, Map<String, Object> param) {
        baseMapper.selectPage(page, param);
    }

    public void doSave(Area area)
    {
        area.insertOrUpdate();
    }

    public void deleteById(Long id)
    {
        baseMapper.deleteById(id);
    }

    //获取省市区树结构
    public Object getAreaTree() {
        List<Area> list = baseMapper.findByLevelType();
        List<Tree<Long>> tree = TreeUtil.build(list, 0L, new TreeNodeConfig().setChildrenKey("children").setIdKey("id").setParentIdKey("parentId").setNameKey("name"),
        (vo, treeNode) -> {
            treeNode.put("id", vo.getId());
            treeNode.put("value", vo.getId());
            treeNode.put("parentId", vo.getParentId());
            treeNode.put("text", vo.getName());
        });
        return tree;
    }
}
